package com.hluck.navigationviewmodel

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.hluck.navigationviewmodel.databinding.FragmentMasterBinding


class MasterFragment : Fragment() {

    private val viewModel by activityViewModels<MyViewModel>()
    private lateinit var mBinding:FragmentMasterBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        mBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_master, container, false)
        mBinding.viewModel = viewModel
        mBinding.lifecycleOwner = activity

        mBinding.seekBar.progress = viewModel.number.value ?: 0

        mBinding.btnMaster.setOnClickListener {
            Navigation.findNavController(it).navigate(R.id.action_masterFragment_to_detailFragment)
        }

        mBinding.seekBar.setOnSeekBarChangeListener(object :OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel.changeNumber(progress)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }

        })


        return mBinding.root
    }

}